﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;
using System.Xml;
using System.Collections;
using System.Reflection;

namespace MDIMain
{
	public partial class MainForm : Apq.Windows.Forms.ImeForm
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			#region 读取标题
			string cfgText = GlobalObject.XmlConfigChain[this.GetType(), "ProgTitle"];
			if (cfgText != null)
			{
				Text = cfgText;
			}
			#endregion

			#region 加载菜单
			Menu_Reload();
			#endregion
		}

		private static bool EnableFirstAutoOpen = true;
		private static int MenuLoadTimes = 0;

		/// <summary>
		/// 重新加载菜单
		/// </summary>
		public static void Menu_Reload()
		{
			GlobalObject.MainForm.menuStrip1.Items.Clear();

			// 读取配置文件
			XmlElement xeMenu = GlobalObject.XmlAsmConfig.GetXmlSection("Menu");

			if (xeMenu != null)
			{
				try
				{
					EnableFirstAutoOpen = Apq.Convert.ChangeType<bool>(xeMenu.Attributes["EnableFirstAutoOpen"].Value, true);
				}
				catch { }
				CreateMenu(null, xeMenu);
				MenuLoadTimes++;
			}
		}

		/// <summary>
		/// [递归]根据配置创建菜单
		/// </summary>
		/// <param name="xn"></param>
		private static void CreateMenu(ToolStripMenuItem Parent, XmlElement xn)
		{
			XmlNodeList xnl = xn.SelectNodes("mi");

			foreach (XmlElement xe in xnl)
			{
				ToolStripMenuItem tsmi = new ToolStripMenuItem();

				#region Text
				try
				{
					tsmi.Text = xe.Attributes["Text"].Value;
				}
				catch { }
				#endregion

				#region ShortcutKeys
				try
				{
					string strShortcutKeys = xe.Attributes["ShortcutKeys"].Value;

					string[] aryShortcutKeys = strShortcutKeys.Split('+');

					Keys k = Keys.None;
					foreach (string str in aryShortcutKeys)
					{
						if (str.Equals("ctrl", StringComparison.OrdinalIgnoreCase))
						{
							k |= Keys.Control;
						}
						else
						{
							k |= (Keys)Enum.Parse(typeof(Keys), str);
						}
					}
					tsmi.ShortcutKeys = k;
				}
				catch { }
				#endregion

				#region Enabled
				try
				{
					tsmi.Enabled = Apq.Convert.ChangeType<bool>(xe.Attributes["Enabled"].Value);
				}
				catch { }
				#endregion

				#region Visible
				try
				{
					tsmi.Visible = Apq.Convert.ChangeType<bool>(xe.Attributes["Visible"].Value, true);
				}
				catch { }
				#endregion

				#region 利用Tag记录其它信息
				Hashtable ht = new Hashtable();

				try
				{
					ht["OpenType"] = xe.Attributes["OpenType"].Value;
				}
				catch { }
				try
				{
					ht["WindowClass"] = xe.Attributes["WindowClass"].Value;
				}
				catch { }
				try
				{
					ht["AutoOpen"] = xe.Attributes["AutoOpen"].Value;
				}
				catch { }
				try
				{
					ht["Single"] = xe.Attributes["Single"].Value;
				}
				catch { }

				tsmi.Tag = ht;
				#endregion

				#region 设置Click事件
				tsmi.Click += new EventHandler(tsmi_Click);
				#endregion

				if (Parent != null)
				{
					Parent.DropDownItems.Add(tsmi);
				}
				else
				{
					GlobalObject.MainForm.menuStrip1.Items.Add(tsmi);
				}

				//处理自动打开
				bool AutoOpen = Apq.Convert.ChangeType<bool>(ht["AutoOpen"]);
				if (AutoOpen
					&& !((MenuLoadTimes == 0) && !EnableFirstAutoOpen)//非 首次加载且不启用首次AutoOpen
				)
				{
					tsmi_Click(tsmi);
				}

				//处理子级
				CreateMenu(tsmi, xe);
			}
		}

		// 菜单项Click事件
		private static void tsmi_Click(object sender, EventArgs e)
		{
			ToolStripMenuItem tsmi = sender as ToolStripMenuItem;
			if (tsmi != null)
			{
				tsmi_Click(tsmi);
			}
		}

		// 按配置打开菜单项设置的窗口
		private static void tsmi_Click(ToolStripMenuItem tsmi)
		{
			Hashtable ht = tsmi.Tag as Hashtable;
			int OpenType = Apq.Convert.ChangeType<int>(ht["OpenType"], 1);
			string WindowClass = Apq.Convert.ChangeType<string>(ht["WindowClass"]);
			if (string.IsNullOrWhiteSpace(WindowClass))
			{
				return;
			}

			bool Single = Apq.Convert.ChangeType<bool>(ht["Single"]);
			string[] aryWin = WindowClass.Split(',');

			// 反射创建窗口
			Form win = null;
			try
			{
				Assembly asm = GlobalObject.TheAssembly;
				if (aryWin.Length > 1)
				{
					// 加载程序集
					asm = Assembly.LoadFrom(aryWin[1]);
				}
				string strClass = aryWin[0];
				string[] aryClass = strClass.Split(':');
				Type twin = asm.GetType(aryClass[0]);
				if (twin != null)
				{
					if (aryClass.Length > 1)
					{
						// 调用类中的方法(静态)
						MethodInfo mi = twin.GetMethod(aryClass[1], BindingFlags.Static | BindingFlags.Public);
						if (mi != null)
						{
							mi.Invoke(null, null);
						}
					}
					else if (Single)
					{
						win = (Form)Apq.Windows.Forms.SingletonForms.GetInstance(twin);
					}
					else
					{
						win = (Form)Activator.CreateInstance(twin);
					}
				}
			}
			catch (Exception ex)
			{
				Apq.GlobalObject.ApqLog.Error(string.Format("创建窗口[0]失败", aryWin[0]), ex);
			}

			if (win != null)
			{
				switch (OpenType)
				{
					case 1:
					default:
						DockContent dc = win as DockContent;
						if (dc != null)
						{
							dc.Show(GlobalObject.MainForm.dockPanel1);
						}
						break;
					case 2:
						win.Show(GlobalObject.MainForm);
						break;
					case 3:
						win.ShowDialog(GlobalObject.MainForm);
						break;
				}
			}
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
		}
	}
}